package com.paic.mhis.hcpms.agreement.healthcareprovider.validator;

import java.util.Map;

import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import com.paic.mhis.hcpms.agreement.healthcareprovider.util.HealthCareProviderConstants;
import com.paic.mhis.hcpms.common.validator.HcpmsBaseValidator;
import com.paic.mhis.hcpms.common.validator.MapValidator;
import com.paic.mhis.hcpms.common.validator.ValidatorList;
import com.paic.pafa.app.biz.bo.validation.ValidStatus;

@Component("providerValidator")
@Scope("prototype")
public class ProviderValidator extends HcpmsBaseValidator {

	@Override
	public Map<Object, Object> validate(Map<Object, Object> fieldMap) {
		validatorList = new ValidatorList();
		validatorList.setValidateAll(true);

		// 常规验证
		// validateFieldNotNull();
		// validateDateFormat();
		validateFieldLengthLimit();
		// validateInteger();

		ValidStatus validStatus = validatorList.validate(fieldMap);

		return throwErrorMassage(validStatus);
	}

	/* 验证非空字段 */
	private void validateFieldNotNull() {
		validatorList.add(MapValidator.require(
				HealthCareProviderConstants.NAME_REQUIRE_VALIDATE,
				HealthCareProviderConstants.NAME_REQUIRE_VALIDATE_MSG));
		validatorList.add(MapValidator.require(
				HealthCareProviderConstants.ORGCODE_REQUIRE_VALIDATE,
				HealthCareProviderConstants.ORGCODE_REQUIRE_VALIDATE_MSG));
		validatorList.add(MapValidator.require(
				HealthCareProviderConstants.LEALPERSON_REQUIRE_VALIDATE,
				HealthCareProviderConstants.LEALPERSON_REQUIRE_VALIDATE_MSG));
		validatorList.add(MapValidator.require(
				HealthCareProviderConstants.REGTIME_REQUIRE_VALIDATE,
				HealthCareProviderConstants.REGTIME_REQUIRE_VALIDATE_MSG));
		validatorList.add(MapValidator.require(
				HealthCareProviderConstants.BEDNUM_REQUIRE_VALIDATE,
				HealthCareProviderConstants.BEDNUM_REQUIRE_VALIDATE_MSG));
		validatorList.add(MapValidator.require(
				HealthCareProviderConstants.ECONOMICTYPE_REQUIRE_VALIDATE,
				HealthCareProviderConstants.ECONOMICTYPE_REQUIRE_VALIDATE_MSG));
		validatorList.add(MapValidator.require(
				HealthCareProviderConstants.HOSPITALLEV_REQUIRE_VALIDATE,
				HealthCareProviderConstants.HOSPITALLEV_REQUIRE_VALIDATE_MSG));

	}

	/* 验证长度限制字段 */
	private void validateFieldLengthLimit() {
		validatorList.add(MapValidator.minAndMaxLength(
				HealthCareProviderConstants.FILENO_VALIDATE_LENGTH, 0, 16,
				HealthCareProviderConstants.FILENO_VALIDATE_LENGTH_MSG));
		validatorList.add(MapValidator.minAndMaxLength(
				HealthCareProviderConstants.NAME_REQUIRE_LENGTH, 0, 60,
				HealthCareProviderConstants.NAME_REQUIRE_LENGTH_MSG));
		validatorList.add(MapValidator.minAndMaxLength(
				HealthCareProviderConstants.LEALPERSON_REQUIRE_LENGTH, 0, 30,
				HealthCareProviderConstants.LEALPERSON_REQUIRE_LENGTH_MSG));
		validatorList.add(MapValidator.minAndMaxLength(
				HealthCareProviderConstants.ORGCODE_REQUIRE_LENGTH, 0, 10,
				HealthCareProviderConstants.ORGCODE_REQUIRE_LENGTH_MSG));
		validatorList.add(MapValidator.minAndMaxLength(
				HealthCareProviderConstants.ADDR_REQUIRE_LENGTH, 0, 50,
				HealthCareProviderConstants.ADDR_REQUIRE_LENGTH_MSG));
		validatorList.add(MapValidator.minAndMaxLength(
				HealthCareProviderConstants.LICENSE_REQUIRE_LENGTH, 0, 32,
				HealthCareProviderConstants.LICENSE_REQUIRE_LENGTH_MSG));
		validatorList.add(MapValidator.minAndMaxLength(
				HealthCareProviderConstants.EMAIL_REQUIRE_LENGTH, 0, 40,
				HealthCareProviderConstants.EMAIL_REQUIRE_LENGTH_MSG));
		validatorList.add(MapValidator.minAndMaxLength(
				HealthCareProviderConstants.TRANSIT_REQUIRE_LENGTH, 0, 100,
				HealthCareProviderConstants.TRANSIT_REQUIRE_LENGTH_MSG));
		validatorList.add(MapValidator.minAndMaxLength(
				HealthCareProviderConstants.POSTCODE_REQUIRE_LENGTH, 0, 7,
				HealthCareProviderConstants.POSTCODE_REQUIRE_LENGTH_MSG));
	}

	/* 验证日期字段 */
	private void validateDateFormat() {
		validatorList.add(MapValidator.date(
				HealthCareProviderConstants.REGTIME_FORMAT_VALIDATE,
				HealthCareProviderConstants.REGTIME_FORMAT_VALIDATE_MSG));
	}

	/* 验证整数字段 */
	private void validateInteger() {
		validatorList.add(MapValidator.integer(
				HealthCareProviderConstants.BEDNUM_INTEGER_VALIDATE, 99999999,
				HealthCareProviderConstants.BEDNUM_INTEGER_VALIDATE_MSG));
	}

}
